package cn.icanci.ladybird.common.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author: icanci
 * @date: Created in 2020/9/29 14:59
 * @classAction: 缓存
 */
@SuppressWarnings("all")
@Component
public class BaseCache {
    // 十分钟的一个缓存
    private Cache<String, Object> tenMinuteCache = CacheBuilder
            .newBuilder()
            // 应该设置合理，后期会扩容
            .initialCapacity(10)
            // 最大值
            .maximumSize(100)
            // 并发数
            .concurrencyLevel(5)
            // 缓存过期时间，写入后十分钟
            .expireAfterWrite(600, TimeUnit.SECONDS)
            // 统计缓存命中率
            .recordStats()
            .build();

    /**
     * 一个小时的缓存
     */
    private Cache<String, Object> oneHourCache = CacheBuilder
            .newBuilder()
            // 应该设置合理，后期会扩容
            .initialCapacity(10)
            // 最大值
            .maximumSize(100)
            // 并发数
            .concurrencyLevel(5)
            // 缓存过期时间，写入后1小时
            .expireAfterWrite(1, TimeUnit.HOURS)
            // 统计缓存命中率
            .recordStats()
            .build();

    public BaseCache() {
    }

    public Cache<String, Object> getOneHourCache() {
        return oneHourCache;
    }

    public void setOneHourCache(Cache<String, Object> oneHourCache) {
        this.oneHourCache = oneHourCache;
    }

    public Cache<String, Object> getTenMinuteCache() {
        return tenMinuteCache;
    }

    public void setTenMinuteCache(Cache<String, Object> tenMinuteCache) {
        this.tenMinuteCache = tenMinuteCache;
    }
}
